<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
	<base href="<%=basePath%>">
	<title>更新单位</title>
	<script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
	<script src='<%=basePath%>dwrsystem/interface/unitService.js'></script>
	<script src='<%=basePath%>dwrsystem/engine.js'></script>
	<script src="<%=basePath%>js/validateJS.js"></script>
	<script src="<%=basePath%>js/tableForm.js"></script>
</head>

<body onload="bodyInit()">
	<div id="dataObj">
	<div id="displayDiv" class="formlayout" style="overflow:auto;width:100%;height:100%;">
		<c:if test="${!empty unitEdit}">
		<form action="<%=basePath%>system-unit/UnitAction!unitEdit.action" method="post" name="editForm">
			<input type="hidden" name="tunit.unitId" id="tunit.unitId" value="${unitEdit.unitId }"/>
			<table>
				<tr>	
					<th>单位名称</th>
					<td> 
						${unitEdit.unitName }
						<input type="hidden" name="editFormHidden"
						 value="[{type:'input',id:'tunit.unitName',name:'tunit.unitName',value:'${unitEdit.unitName }',showText:'${unitEdit.unitName }',maxlength:'25',mustEnter:'yes'}]"/>
					</td>
				</tr>
				<tr>	
					<th>所属工作站</th>
					<td>
						${unitEdit.siteName }
						<input type="hidden" name="editFormHidden"
						 value="[{type:'customize',showText:'${unitEdit.siteName }',customizeId:'customizeDIV_worksite'}]"/>
					</td>
				</tr>
				<tr>	
					<th>单位类型</th>
					<td>
					<c:if test="${unitEdit.unitType == 1}">普通类型</c:if><c:if test="${unitEdit.unitType == 2}">其它类型</c:if>
					<input type="hidden" name="editFormHidden"
						value="[{type:'select',id:'tunit.unitType',name:'tunit.unitType',value:'1&普通类型|2&其他类型',defaultSelect:'${unitEdit.unitType }',
							showText:'<c:if test="${unitEdit.unitType == 1}">普通类型</c:if><c:if test="${unitEdit.unitType == 2}">其它类型</c:if>'}]"/>					
					</td>
				</tr>
				<tr>	
					<th>单位状态</th>
					<td>
						<c:if test="${unitEdit.status == 1}">有效</c:if><c:if test="${unitEdit.status == 0}">无效</c:if>
						<input type="hidden" name="editFormHidden"
							value="[{type:'select',id:'tunit.status',name:'tunit.status',value:'1&有效|0&无效',defaultSelect:'${unitEdit.status }',
								showText:'<c:if test="${unitEdit.status == 1}">有效</c:if><c:if test="${unitEdit.status == 0}">无效</c:if>'}]"/>							
					</td>
				</tr>
				<tr>   			
					<th>备注</th>
					<td>
						${unitEdit.note }
						<input type="hidden" name="editFormHidden"
						 value="[{type:'textarea',id:'tunit.note',name:'tunit.note',value:'${unitEdit.note }',showText:'${unitEdit.note }'}]"/>
					</td>
				</tr>
			</table>
		</form>
 		</c:if>			
	</div>
	<div id="customizeDIV_worksite" style="display:none;">
		 <input name="showWorksiteName" id="showWorksiteName" value="${unitEdit.siteName }" readonly="readonly"/>
		 <input type="hidden" name="tunit.worksiteId" id="tunit.worksiteId" value="${unitEdit.worksiteId }"/>
		 <button onclick="queryWorksites();return false;">选择</button>
		 <button onclick="clearWorksite();return false;">清除</button>	
	</div>	
	<div id="delDiv" style="display:none"><!-- 删除信息 -->
		<form action="<%=basePath%>system-unit/UnitAction!unitDelFromTree.action" method="post" name="delForm">
			<input type="hidden" name="unitId" id="unitId" value="${unitEdit.unitId }"/>
		</form>
	</div>
	</div>
</body>
	
<script type="text/javascript">
var mytree;
var uids; //更新单位的id集合，用于更新单位无效时的颜色
var dhxLayout = createLayoutNormal('1C',false,true);

function bodyInit(){
	configLayout();
	mytree = parent.parent.window.MyTree;
}

function configLayout(){
	var a = dhxLayout.cells('a');
	a.hideHeader();
	a.setText('主页面');
	a.fixSize(true, true);
	if(power.getPower() != '' && (power.getPowerInsert() || power.getPowerDelete()))
		createToolbarFromDHXLayoutCell(a,function(toolbar){
		    toolbar.addText("info", 1, "操作:");
		    toolbar.addSeparator("sep1", 2);
		    if(power.getPowerInsert())toolbar.addButton("add", 3, "新增下级单位", "new.gif", "new_dis.gif");
		    if(power.getPowerUpdate()){
		    	toolbar.addButton("update", 4, "修改", "settings.gif", "settings.gif");
		    	toolbar.addButton("forUpdate", 6, "提交", "save.gif", "save_dis.gif");
		    	toolbar.addButton("cancel", 7, "取消", "undo.gif", "undo_dis.gif");
		    	toolbar.hideItem('forUpdate');
		    	toolbar.hideItem('cancel');
		    }
    		if(power.getPowerDelete())toolbar.addButton("delete", 8, "删除", "cut.gif", "cut_dis.gif");
		    toolbar.attachEvent("onClick", function(id) {
		    	if(id == 'add'){
		    		addUnit();
		    	}
		    	if(id == 'delete'){
		    		validateDel();
		    	}
		    	if(id == 'update'){
		    		//updateUnit();
		    		tableForm.tableToForm();
		    		toolbar.showItem('forUpdate');
		    		toolbar.showItem('cancel');
		    		toolbar.hideItem('update');
		    		
		    	}
		    	if(id == 'cancel'){
		    		tableForm.formReturnTable();
		    		toolbar.hideItem('forUpdate');
		    		toolbar.hideItem('cancel');
		    		toolbar.showItem('update');
		    	}
		    	if(id == 'forUpdate'){
		    		validateEdit();
		    	}
		    });
		});
	a.attachObject('dataObj');
}

function validateEdit(){
	
	var uname = document.getElementById('tunit.unitName').value;
	if(trim(uname)==''){parent.parent.parent.notifyWarn('请输入单位名称');return;}
	if (!validatePattern.validateInputZZ(uname,false)){
		parent.parent.parent.notifyWarn('单位名称非法，输入只能是[汉字,数字、26个英文字母或者下划线]');
		return ;
	}

	var uid = document.getElementById('tunit.unitId').value;
	var ustatus = document.getElementById('tunit.status').value;
	layoutProgressOn(dhxLayout);
	unitService.hasTUnitName({unitId:uid, unitName:uname}, function(data){
		if(data == 1){
			layoutProgressOff(dhxLayout);
			parent.parent.parent.notifyWarn('已存在相同单位名称');
			return;
		}else{
			if(ustatus != '${unitEdit.status}'){
				unitService.updateTUnitForStatusByPK({unitId:uid, status:ustatus}, function(data){
					uids = data;
					document.forms["editForm"].submit();
					changeNodeColor();
					mytree.editNode(uid, uname);
				});
			}else{
				document.forms["editForm"].submit();
				mytree.editNode(uid, uname);
			}
		}
	});		
}

function changeNodeColor(){
	for(var i = 0; i < uids.length; i++){
		if(document.getElementById('tunit.status').value == 0){
			mytree.changeColor(uids[i], 'red');
		}else{
			mytree.changeColor(uids[i], 'black');
		}
	}
}

function validateDel(){
	confirmInfo('删除本单位，会同时删除其下级单位',
		function(choose){
			if('ok' == choose){
				layoutProgressOn(dhxLayout);
				var uid = document.getElementById('tunit.unitId').value;
				document.forms["delForm"].submit();
				if(mytree.getNodeLevel(uid) == 1){
					mytree.delNode(uid);
					parent.parent.window.changeRightPage('<%=basePath%>page/system/unit/unit_listtop.jsp?superUnitId=0');
				}else{
					mytree.delNode(uid);
				}
			}else{
				return;
			}
		}
	);
}

function queryWorksites(){
	openNewWindowURL('选择工作站','<%=basePath%>page/system/unit/unitWorksite_list.jsp');
}

function clearWorksite(){
	document.getElementById('showWorksiteName').value = '';
	document.getElementById('tunit.worksiteId').value = '';
}

function addUnit(){
	layoutProgressOn(dhxLayout);
	var uid = document.getElementById('tunit.unitId').value;
	document.location.href='<%=basePath%>page/system/unit/unit_add.jsp?unitId=' + uid + '&nowurl=edit';
}
</script>
	
</html>
